<?php
/**
 * Created by PhpStorm.
 * User: 悟能科技
 * Date: 2018/10/12
 * Time: 10:18
 */

namespace app\macht\controller;


use app\common\controller\AdminBase;
use app\common\controller\Platurl;
use app\common\controller\Ydhwork;
use app\common\model\CardMember;
use app\common\model\Paylog;
use app\common\model\YdhOpen;
use app\common\model\YdhUser;
use app\common\model\YdhUserCate;
use think\Db;
use think\Request;
use think\Session;
use think\Controller;
class Ydhmarke extends MachBase
{
    //
    public function ydhindex(){

        return $this->fetch();
    }

    //
    public function userlist(Request $request){
        if ($request->isAjax()) {
            $param = $request->post();
//            return json($param);
            //免费  /  付费
            if (!empty($param['level'])) {
                $map['a.customertypeId'] = $param['level'];
            }
            //卡号   编号
            if (!empty($param['code'])) {
                $map['a.code'] = $param['code'];
            }
            //昵称
            if (!empty($param['nickname'])) {
                $map['a.name'] = $param['nickname'];
            }
            //手机
            if (!empty($param['phone'])) {
                $map['a.mobile'] = $param['phone'];
            }

            //时间
            if (!empty($param['search_date'])) {
                $date = explode(" - ", $param['search_date']);
                $start_time = strtotime($date[0]);
                $end_time = strtotime($date[1]);
                $map['a.create_time'] = array(array('egt', $start_time), array('elt', $end_time), "and");
            }

            $map['a.top_mid'] = Session::get('login_top_mid');

            $YdhUser = new YdhUser();
            $list = $YdhUser->alias('a')->where($map)->limit(($param['pageNumber'] - 1) * $param['pageSize'], $param['pageSize'])->select();
            $lists = [];
            foreach ($list as $k=>$v){
                $lists[$k] = $v;
                $lists[$k]['user_type'] = $this->get_usettype($v['customertypeId']);
                $lists[$k]['statu'] = $v['statu'] == 1?'正常':'异常';
            }
            $re_param["total"] = $YdhUser->alias('a')->where($map)->count();
            //获取的记录
            $re_param["rows"] = $lists;
            return json($re_param);
        }else{
            $YdhUserCate = new YdhUserCate();
            $map['top_mid'] = Session::get('login_top_mid');
            $catelist = $YdhUserCate->where($map)->select();
            $this->assign('catelist',$catelist);
            return $this->fetch();
        }
    }

    public function orderlist(Request $request){
        if ($request->isAjax()) {
            $param = $request->post();

            //免费  /  付费
            if (!empty($param['orderid'])) {
                $map['a.orderid'] = $param['orderid'];
            }
            if ($param['pay_statu'] === '0') {
                $map['a.pay_statu'] = 0;
            }else {
                if (!empty($param['pay_statu'])) {
                    $map['a.pay_statu'] = $param['pay_statu'];
                }
            }
            //时间
            if (!empty($param['search_date'])) {
                $date = explode(" - ", $param['search_date']);
                $start_time = strtotime($date[0]);
                $end_time = strtotime($date[1]);
                $map['a.add_time'] = array(array('egt', $start_time), array('elt', $end_time), "and");
            }
            $map['a.mid'] = ['in', $this->get_mer_mid()];
            $map['a.attach']  = 'ydh_upgrade';
            $Paylog = new Paylog();
            $list = $Paylog->alias('a')
                ->join('wn_ydh_user b', 'a.openid=b.openid', 'left')->
                where($map)->limit(($param['pageNumber'] - 1) * $param['pageSize'], $param['pageSize'])
                ->field('a.orderid,a.price,a.source,a.pay_statu,pay_fs,a.pay_cate,a.add_time,a.service_charge,b.name,b.code,b.remark')
                ->select();
            $lists = [];
            foreach ($list as $k=>$v){
                $lists[$k] = $v;
                $lists[$k]['pay_fs'] = getpayfs($v['pay_fs']);
                $lists[$k]['pay_statu'] = getpaystatus($v['pay_statu']);
                $lists[$k]['add_time'] = date('Y-m-d H:i:s', $v['add_time']);

            }
            $re_param["total"] = $Paylog->alias('a')->where($map)->count();
            //获取的记录
            $re_param["rows"] = $lists;
            return json($re_param);
        }else{

            return $this->fetch();
        }
        return $this->fetch();
    }

    public function usercatelist(Request $request){
        $YdhUserCate = new YdhUserCate();
        $YhdOpen = new YdhOpen();
        $map['top_mid'] = Session::get('login_top_mid');
        $openinfo = $YhdOpen->where($map)->field('wx_open_id,is_cate')->find();
        if ($request->isAjax()) {
            $param = $request->post();

            if ($param['get_type'] == 'get_list'){
                //获取的记录
                $list = $YdhUserCate->where($map)->select();
                $re_param["total"] = $YdhUserCate->where($map)->count();
                $re_param["rows"] = collection($list)->toArray();
                return json($re_param);
            }elseif($param['get_type'] == 'get_sync'){

                if ($openinfo['is_cate'] == 0){
                    $Ydhwork = new Ydhwork();
                    $res = $Ydhwork->get_user_cate($map['top_mid']);
//                    return json($res);
                    if ($res['code'] == 200) {
                        if ($res['data']['items']) {
                            $cateitems = $res['data']['items'];
                            foreach ($cateitems as $k => $v) {
                                $data[$k]['wx_open_id'] = $openinfo['wx_open_id'];
                                $data[$k]['top_mid'] = Session::get('login_top_mid');
                                $data[$k]['discountrate'] = $v['discountrate'];
                                $data[$k]['level'] = $v['level'];
                                $data[$k]['name'] = $v['name'];
                                $data[$k]['isDefault'] = $v['isDefault'];
                                $data[$k]['cate_id'] = $v['id'];
                                $data[$k]['create_time'] = time();
                            }
                            $YdhUserCate->allowField(true)->saveAll($data);
                            $YhdOpen->save(['is_cate' => 1], $map);
//                            $this->redirect('merchant/ydhmarke/usercatelist');
                            $re_param['code'] = 0;
                            $re_param['message'] = '设置成功';
                        } else {
//                            $this->redirect('merchant/ydhmarke/usercatelist');
                            $re_param['code'] = 9006;
                            $re_param['message'] = '没有设置列表';
                        }
                    }else{
//                        $this->redirect('merchant/ydhmarke/usercatelist');
                        $re_param['code'] = 9001;
                        $re_param['message'] = $res['message'];
                    }
                }else{
//                    $this->redirect('merchant/ydhmarke/usercatelist');
                    $re_param['code'] = 9003;
                    $re_param['message'] = '已同步';
                }
                return json($re_param);
            }

        }else{

            return $this->fetch();
        }
    }

    public function ydhsetup(Request $request){
        $YdhOpen = new YdhOpen();
        $map['top_mid'] = Session::get('login_top_mid');
        $info = $YdhOpen->where($map)->find();
//        var_dump($YdhOpen->getLastSql());
//        var_dump($info);
        if ($request->isAjax()) {
            $param = $request->post();
            $data = $param;
            $data['grant_type'] = 'client_credentials';
            $data['scope'] = 'basic';
//            return json($data);
            if ($info){
                $res = $YdhOpen->allowField(true)->save($data,$map);
            }else {
                $data['wx_open_id'] = Db::name('Momerchant')->where('id',$map['top_mid'])->value('wx_open_id');
                $data['top_mid'] = Session::get('login_top_mid');
                $res = $YdhOpen->allowField(true)->save($data);
            }
            if ($res){
                $rarr['code'] = 0;
                $rarr['message'] = '设置成功';
            }else{
                $rarr['code'] = 9001;
                $rarr['message'] = '设置失败';
            }
            return json($rarr);
        }else{
            $YdhUserCate = new YdhUserCate();
            $map['top_mid'] = Session::get('login_top_mid');
            $catalist = $YdhUserCate->where($map)->select();
            $Platurl = new Platurl();
            $agent_url = $Platurl->getagenturl('top_mid', $info['top_mid']);
            $info['ydh_index_url'] = $agent_url . '/mobile.php?s=/ordergoods/index/card_id/' . $this->get_card_id();     //分享
            $this->assign('catelist',$catalist);
            $this->assign('info',$info);
            return $this->fetch();
        }
    }

    //
    public function get_card_id(){
        $CardMember = new CardMember();
        $info = $CardMember->where('top_mid',Session::get('login_top_mid'))->value('card_id');
        return $info;
    }

    //客户类型
    public function get_usettype($id){
        $YdhUserCate = new YdhUserCate();
        $map['cate_id'] = $id;
        $map['top_mid'] = Session::get('login_top_mid');
        $info = $YdhUserCate->where($map)->value('name');
        if (!$info){
            $info = '未知';
        }
        return $info;
    }
}